﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Controls;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMMaterialByMatUse : BasePage
    {
        #region 变量定义
        int m_count = 0;
        private string m_strMainKey = "ScheduleMatID";
        #endregion

        #region 属性定义

        private DataTable DtDtMatUseData
        {
            get
            {
                if (null == Session["DtMatUseData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["DtMatUseData"];
                }
            }
            set
            {
                Session["DtMatUseData"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillDetailID");//0
            dt.Columns.Add("ScheduleMatID");//1 
            dt.Columns.Add("MaterialID");//2 
            dt.Columns.Add("MaterialName");//4
            dt.Columns.Add("Specifications");//7
            dt.Columns.Add("Unit");//8
            dt.Columns.Add("ChangeNum");//5 
            dt.Columns.Add("NeedNums");//9
            dt.Columns.Add("LeftNums");//10
            dt.Columns.Add("StockNums");//11
            dt.Columns.Add("Nums");//12 
            dt.Columns.Add("MMNumber");//12 
            dt.Columns.Add("SingleMMNumber");//12 
            dt.Columns.Add("Remark");//13 
            dt.Columns.Add("IsNew");//14 
            dt.Columns.Add("IsDel");//14
            return dt;
        }
        #endregion

        #region 初始化及绑定控件
        private void BindData(int pageIndex)
        {
            string ScheduleDetailID = hdScheduleDetailID.Value.Trim();
            string BillID = hdID.Value.Trim();
            DataTable dt = BsMatUseBusiness.SelectMaterial(txtMaterialID.Text.Trim(), txtMaterialName.Text.Trim(), ScheduleDetailID, BillID, pageIndex, pcData.PageSize, out m_count);

            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                DataView dv = new DataView(DtDtMatUseData);
                dv.RowFilter = "IsDel<>'1'";
                DataTable dtTemp = dv.ToTable();

                EnableGridViewRow(grdResult, dtTemp, m_strMainKey, false);
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdScheduleDetailID.Value = UIHelper.GetQueryStringValue("ScheduleDetailID");
                hdID.Value = UIHelper.GetQueryStringValue("id");
                InitDDL();
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                BindData(0);
            }
        }

        private void InitDDL()
        {
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success',1);CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }
        #endregion

        #region 本地方法
        private void AddPro()
        {
            DtDtMatUseData = GetAddPro(DtDtMatUseData).Copy();
        }

        private DataTable GetAddPro(DataTable dt)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblScheduleMatID = gvr.FindControl("lblScheduleMatID") as Label;
                    Label lblSingleMMNumber = gvr.FindControl("lblSingleMMNumber") as Label;
                    Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;
                    Label lblMaterialName = gvr.FindControl("lblMaterialName") as Label;
                    Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                    Label lblUnit = gvr.FindControl("lblUnit") as Label;
                    Label lblChangeNum = gvr.FindControl("lblChangeNum") as Label;
                    Label lblNeedNums = gvr.FindControl("lblNeedNums") as Label;
                    Label lblLeftNums = gvr.FindControl("lblLeftNums") as Label;
                    Label lblStockNums = gvr.FindControl("lblStockNums") as Label;

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        DataRow[] drs = dtTemp.Select("ScheduleMatID='" + lblScheduleMatID.Text.Trim() + "'  ");
                        if (drs.Length == 0)
                        {
                            DataRow dr = dtTemp.NewRow();
                            dr["ScheduleMatID"] = lblScheduleMatID.Text.Trim();
                            dr["SingleMMNumber"] = lblSingleMMNumber.Text.Trim();
                            dr["MaterialID"] = lblMaterialID.Text.Trim();
                            dr["MaterialName"] = lblMaterialName.Text.Trim();
                            dr["Specifications"] = lblSpecifications.Text.Trim();
                            dr["Unit"] = lblUnit.Text.Trim();
                            dr["ChangeNum"] = lblChangeNum.Text.Trim();
                            dr["NeedNums"] = lblNeedNums.Text.Trim();
                            dr["LeftNums"] = lblLeftNums.Text.Trim();
                            dr["Nums"] = lblLeftNums.Text.Trim();
                            dr["StockNums"] = lblStockNums.Text.Trim();
                            dr["SingleMMNumber"] = lblSingleMMNumber.Text.Trim();
                            decimal SingleMMNumber = CommonMethod.ConvertToDecimal(lblSingleMMNumber.Text.Trim());
                            decimal LeftNums = CommonMethod.ConvertToDecimal(lblLeftNums.Text.Trim());
                            dr["MMNumber"] = Math.Round(SingleMMNumber * LeftNums, 2);
                            dr["IsDel"] = "-1";
                            dr["IsNew"] = "1";
                            dtTemp.Rows.Add(dr);
                        }
                        else if (drs.Length > 0 && CommonMethod.GetString(drs[0]["IsDel"]) == "1")
                        {
                            drs[0]["ScheduleMatID"] = lblScheduleMatID.Text.Trim();
                            drs[0]["SingleMMNumber"] = lblSingleMMNumber.Text.Trim();
                            drs[0]["MaterialID"] = lblMaterialID.Text.Trim();
                            drs[0]["MaterialName"] = lblMaterialName.Text.Trim();
                            drs[0]["Specifications"] = lblSpecifications.Text.Trim();
                            drs[0]["Unit"] = lblUnit.Text.Trim();
                            drs[0]["ChangeNum"] = lblChangeNum.Text.Trim();
                            drs[0]["NeedNums"] = lblNeedNums.Text.Trim();
                            drs[0]["LeftNums"] = lblLeftNums.Text.Trim();
                            drs[0]["Nums"] = lblLeftNums.Text.Trim();
                            drs[0]["StockNums"] = lblStockNums.Text.Trim();
                            drs[0]["SingleMMNumber"] = lblSingleMMNumber.Text.Trim();
                            decimal SingleMMNumber = CommonMethod.ConvertToDecimal(lblSingleMMNumber.Text.Trim());
                            decimal LeftNums = CommonMethod.ConvertToDecimal(lblLeftNums.Text.Trim());
                            drs[0]["MMNumber"] = Math.Round(SingleMMNumber * LeftNums, 2);
                            drs[0]["IsDel"] = "-1";
                        }
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select("ScheduleMatID='" + lblScheduleMatID.Text.Trim() + "'  ");
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        #endregion
    }
}
